import sys
sys.setrecursionlimit(5000)
class Logger(object):
    def __init__(self, filename="Default.log"):
        self.terminal = sys.stdout
        self.log = open(filename, "a")

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)

    def flush(self):
        pass
def clean(file):
    filename = '21st-century\Mad_Men\Mad.Men.S01E01.srt'
    with open(filename, encoding='UTF-8') as file_obj:
        for line in file_obj:
            line = line.strip()
            if len(line) and not line.isdigit():
                first_str = line[0:1]
                if not first_str.isdigit():
                    sys.stdout=Logger(file)
            data = open("C:\a.txt", 'w+')
            print(line, file=data)
            data.close()
    with open("C:\a.txt",'r') as f:
        for line in f:
            print(line)

    print('aaaaaa')
    dels = ( '-', '(', ')', '"','<i>', '</i>', '<font color=#00FF00>', '<font color=#00FFFF>',
            '<font'' color=#00FFFF>', '<font'' color="#00ff00">',
            '<font'' color="#ff0000">', '</font>', '#',
'\xe2\x99\xaa', 'www.AllSubs.org', 'http://DJJ.HOME.SAPO.PT/', 'Downloaded',
            'Shared', 'Sync', 'www.addic7ed.com', 'n17t01',)
    fil = open(file, "r")  # 以只读模式读取文件
    lines=fil.read()
    fil.close()
    news=[]
    j=0
    for line in lines:  # 逐行遍历
        for i in range(len(line)):
            if line[i]==' ':
                news.append(line[j:i])
                j=i
                continue
        for i in range(len(dels)):
            if dels[i] in news:
                news.remove(dels[i])
    # 以写的方式打开文件，如果文件不存在，就会自动创建，如果存在就会覆盖原文件
    file_write_obj = open("pip list new.txt", 'w')
    for var in news:
        file_write_obj.writelines(var)
        file_write_obj.writelines('\n')
    file_write_obj.close()

clean('test.txt')
